//
//  YTPhotoPreviewController.m
//  YUNT
//
//  Created by haha on 2017/11/19.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTPhotoPreviewController.h"
#import "YTPhotoLibraryManager.h"

@protocol ResponseSingleTapDelegate <NSObject>

- (void)singleTap;

@end

@interface YTPreviewDisplayCell : UICollectionViewCell<UIScrollViewDelegate>
{
    UIImageView *_imageView;
}
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) YTPhotoLibraryItem *resourceItem;

@property (nonatomic, weak) id<ResponseSingleTapDelegate> delegate;

@end

@implementation YTPreviewDisplayCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.backgroundColor = [UIColor blackColor];
  
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        
        _scrollView.minimumZoomScale = 1.0;
        
        _scrollView.maximumZoomScale = 1.5;
        
        _scrollView.delegate = self;
        
        [self.contentView addSubview:_scrollView];
        
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
        
        doubleTap.numberOfTapsRequired = 2;
        
        [self.contentView addGestureRecognizer:doubleTap];
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
        
        singleTap.numberOfTapsRequired = 1;
        
        [self.contentView addGestureRecognizer:singleTap];
        
        [singleTap requireGestureRecognizerToFail:doubleTap];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _imageView = [[UIImageView alloc] initWithFrame:_scrollView.bounds];
    
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    [_scrollView addSubview:_imageView];
    
    _scrollView.zoomScale = 1.0;
    
    _scrollView.frame = CGRectMake(0, 0, self.bounds.size.width,self.bounds.size.height);
    
    _imageView.image = [UIImage imageWithData:self.resourceItem.resourceData];
}

//清楚单元格的图片
- (void)prepareForReuse
{
    [_imageView removeFromSuperview];
}

#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}

#pragma mark - tap

- (void)singleTapAction:(UITapGestureRecognizer *)sender
{
    if ([self.delegate respondsToSelector:@selector(singleTap)])
    {
        [self.delegate singleTap];
    }
}

- (void)doubleTapAction:(UIGestureRecognizer *)sender
{
    if (_scrollView.zoomScale > 1)
    {
        [_scrollView setZoomScale:1 animated:YES];
        
    }
    else
    {
        CGPoint touchPoint = [sender locationInView:_scrollView];
        
        CGFloat newZoomScale = 2;
        
        CGFloat xsize = self.frame.size.width / newZoomScale;
        
        CGFloat ysize = self.frame.size.height / newZoomScale;
        
        [_scrollView zoomToRect:CGRectMake(touchPoint.x - xsize/2, touchPoint.y - ysize/2, xsize, ysize) animated:YES];
    }
}

@end

@interface YTPhotoPreviewController ()<ResponseSingleTapDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
{
    UICollectionView *_collectionView;
}

@end

@implementation YTPhotoPreviewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    YTPhotoLibraryItem *item = self.imageArray[self.initialPositon.item];
    
    self.title = Language(item.resourceDate);
    
    self.view.backgroundColor  = [UIColor blackColor];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width + 10.0f, [UIScreen mainScreen].bounds.size.height);
    
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.minimumLineSpacing = 0;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(-5.0f, -64.0, [UIScreen mainScreen].bounds.size.width + 10.0f, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:flowLayout];
    
    _collectionView.dataSource = self;
    
    _collectionView.delegate = self;
    
    _collectionView.pagingEnabled = YES;
    
    [_collectionView registerClass:[YTPreviewDisplayCell class] forCellWithReuseIdentifier:@"YTPreviewDisplayCellId"];
    
    [self.view addSubview:_collectionView];
    
    [_collectionView setContentOffset:CGPointMake(self.initialPositon.item * ([UIScreen mainScreen].bounds.size.width + 10), 0)];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
  
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YTPreviewDisplayCell *displayCell = (YTPreviewDisplayCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"YTPreviewDisplayCellId" forIndexPath:indexPath];;
    
    displayCell.delegate = self;

    displayCell.resourceItem = self.imageArray[indexPath.row];
    
    [displayCell layoutSubviews];
    
    return displayCell;
}

#pragma mark -UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
  
}

#pragma mark -ResponseSingleTapDelegate

- (void)singleTap
{
    self.navigationController.navigationBar.hidden = !self.navigationController.navigationBar.hidden;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
